let tableJson = [];

$(function () {
    var id = $.trim(MX.getQueryVariable("id"));
    if (id == null || id === "null" || !id) {
        LayerUtil.failMsg("未找到校准记录，请重新进入")
        return false;
    }

    Table.builder()
        .listUrl(CALIBRATION_RECORD_LIST + "?calibrationId=" + id)
        .deleteUrl(CALIBRATION_RECORD_DETELE)
        .onLoadSuccess(getData)
        .init();

    $("#tableAjaxId").on("click", ".detailInfo", function () {
        $('#jilu').html("");
        var id = $.trim($(this).attr("dataId"));
        var data = tableJson.find(item => item.id === id);
        var jsonData = JSON.parse(data.caliRecordJson)
        for (var i = 0; i < jsonData.length; i++) {
            $("#jilu").append(`<div>${jsonData[i].name}</div>`)
        }
    })
});

function getData(data) {
     tableJson = data.rows;
}

columns = [
    {
        checkbox: true,
        align: 'center'
    }, {
        field: 'stationName',
        title: '校准设备（养殖池）名称',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'speciesName',
        title: '物种名称',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'typeName',
        title: '设备类型',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'status',
        title: '状态',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            if (value === 0) {
                return "<span style='font-weight: bold;'>校准中</span>";
            } else if (value === 1) {
                return "<span style='font-weight: bold;'>校准完成</span>";
            } else if (value === 2) {
                return "<span style='font-weight: bold;'>放弃校准</span>";
            }
        }
    }, {
        field: 'mode',
        title: '校准模式',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            if (value === "normal") {
                return "<span style='font-weight: bold;'>低速模式</span>";
            } else if (value === "high") {
                return "<span style='font-weight: bold;'>高速模式</span>";
            }
        }
    }, {
        field: 'speed',
        title: '校准转速(r/min)',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'circle',
        title: '校准圈数(圈)',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'fittingTimes',
        title: '拟合次数(次)',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'userBdMinute',
        title: '期望值',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return value + "min " + row.userBdGram + "g"
        }
    }, {
        field: 'createTime',
        title: '创建时间',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return DateUtil.changeDateToString(value, DateFormatEnum.YYYY_MM_DD_HH_MM);
        }
    }, {
        field: 'lastModifyTime',
        title: '最后修改时间',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return DateUtil.changeDateToString(value, DateFormatEnum.YYYY_MM_DD_HH_MM);
        }
    }, {
        title: "操作",
        align: 'center',
        valign: 'middle',
        width: 180,
        formatter: option
    }
]

function option(value, row, index) {
    return '<div class="d-flex justify-content-center">' +
        '<a class="btn btn-xs btn-default m-r-5 deletes" data-toggle="modal" data-target=".removeDio" dataId="' + row.id + '"  title="删除" >删除</a>' +
        '<a class="btn btn-xs btn-default detailInfo" data-toggle="modal" data-target="#detailInfoDiv" dataId="' + row.id + '" title="详情" >详情</a>' +
        '</div>';
}